/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.controller;

import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.WmRequestHeader;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.wms.domain.allotmanagementpc.AllotManagementPcService;
import com.lightkits.wms.domain.allotmanagementpc.QueryAllotDocMesService;
import com.lightkits.wms.dto.allotmanagementpc.CancelRequestTicketRequestType;
import com.lightkits.wms.dto.allotmanagementpc.CancelRequestTicketResponseType;
import com.lightkits.wms.dto.allotmanagementpc.GetExecuteLinePcRequestType;
import com.lightkits.wms.dto.allotmanagementpc.GetExecuteLinePcResponseType;
import com.lightkits.wms.dto.allotmanagementpc.GetRequestHeaderRequestType;
import com.lightkits.wms.dto.allotmanagementpc.GetRequestHeaderResponseType;
import com.lightkits.wms.dto.allotmanagementpc.GetRequestLinePcRequestType;
import com.lightkits.wms.dto.allotmanagementpc.GetRequestLineResponseType;
import com.lightkits.wms.dto.allotmanagementpc.SaveExecutionLineRequestType;
import com.lightkits.wms.dto.allotmanagementpc.SaveExecutionLineResponseType;
import com.lightkits.wms.dto.allotmanagementpc.SaveRequestTicketRequestType;
import com.lightkits.wms.dto.allotmanagementpc.SaveRequestTicketResponseType;
import com.lightkits.wms.dto.allotmanagementpc.model.ExecuteLinePcDto;
import com.lightkits.wms.dto.allotmanagementpc.model.RequestHeaderPcDto;
import com.lightkits.wms.dto.allotmanagementpc.model.RequestLinePcDto;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 魏静
 * @create 2019-07-19 15:06
 * @desc 调拨管理平台2.0
 **/
@RestController
@RequestMapping("/AllotManagementPcService")
public class AllotManagementPcController {
    private final QueryAllotDocMesService docMesService;

    @Autowired
    public AllotManagementPcController(QueryAllotDocMesService docMesService) {
        this.docMesService = docMesService;
    }

    /**
     * @param request GetRequestHeaderRequestType
     * @return GetRequestHeaderResponseType
     * @throws Exception 异常
     */


    @PostMapping(value = "/getRequestHeaderMes")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getRequestHeaderMes")
    @ResponseBody
    public GetRequestHeaderResponseType getRequestHeaderMes(
            @RequestBody(required = false) GetRequestHeaderRequestType request) throws Exception {
        GetRequestHeaderResponseType response = new GetRequestHeaderResponseType();
        response.setSuccessful(true);
        List<RequestHeaderPcDto> pcDtos = docMesService.queryAllotHeaderMes(request.getQueryParamsDto());
        response.setHeaderPcDtos(pcDtos);
        return response;
    }

    /**
     * @param request GetRequestLinePcRequestType
     * @return GetRequestLineResponseType
     */
    @PostMapping(value = "/getRequestLineMes")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getRequestLineMes")
    @ResponseBody
    public GetRequestLineResponseType getRequestLineMes(
            @RequestBody @Validated GetRequestLinePcRequestType request) {
        GetRequestLineResponseType response = new GetRequestLineResponseType();
        response.setSuccessful(true);
        List<RequestLinePcDto> pcDtos = docMesService.queryAllotLineMes(request.getRequestHeaderId());
        response.setPcDtos(pcDtos);
        return response;
    }

    @PostMapping(value = "/getExeLineMes")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getExeLineMes")
    @ResponseBody
    public GetExecuteLinePcResponseType getExeLineMes(
            @RequestBody @Validated GetExecuteLinePcRequestType request) {
        GetExecuteLinePcResponseType response = new GetExecuteLinePcResponseType();
        response.setSuccessful(true);
        List<ExecuteLinePcDto> executeLinePcDtos = docMesService.queryExecutionLineMes(request.getRequestLineId());
        response.setLinePcDtos(executeLinePcDtos);
        return response;
    }

    @Autowired
    private AllotManagementPcService managementPcService;

    /**
     * @param request SaveRequestTicketRequestType
     * @return SaveRequestTicketResponseType
     */
    @PostMapping(value = "/saveRequestTicket")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/saveRequestTicket")
    @ResponseBody
    public SaveRequestTicketResponseType saveRequestTicket(
            @RequestBody @Validated SaveRequestTicketRequestType request) {
        SaveRequestTicketResponseType response = new SaveRequestTicketResponseType();
        response.setSuccessful(true);
        IResult result = managementPcService.checkParams(request.getHeaderPcDto(), request.getLinePcDtos());
        if (!result.isSuccessful()) {
            return result.fill(response);
        }
        result = managementPcService.allotManagementPc(request.getHeaderPcDto(), request.getLinePcDtos(), request.getSplitRequestFlag());
        return result.fill(response);
    }

    /**
     * @param request SaveRequestTicketRequestType
     * @return SaveRequestTicketResponseType
     */
    @PostMapping(value = "/saveExecuteLine")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/saveExecuteLine")
    @ResponseBody
    public SaveExecutionLineResponseType saveExecuteLine(
            @RequestBody @Validated SaveExecutionLineRequestType request) {
        SaveExecutionLineResponseType response = new SaveExecutionLineResponseType();
        response.setSuccessful(true);
        IResult result = managementPcService.checkExeParams(request.getExeLinePcDtos(),
                request.getRequestLineMap());
        if (!result.isSuccessful()) {
            return result.fill(response);
        }
        result = managementPcService.allotExecute(request.getExeLinePcDtos(),
                request.getRequestLineMap());
        return result.fill(response);
    }

    /**
     * @param request SaveRequestTicketRequestType
     * @return SaveRequestTicketResponseType
     */
    @PostMapping(value = "/cancelRequestHeader")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/cancelRequestHeader")
    @ResponseBody
    public CancelRequestTicketResponseType cancelRequestHeader(
            @RequestBody @Validated CancelRequestTicketRequestType request) {
        CancelRequestTicketResponseType response = new CancelRequestTicketResponseType();
        response.setSuccessful(true);
        TransactionValidate check = TransactionValidate.beginValidity(WmRequestHeader.class);
        check.checkIdValidity(request.getRequestHeaderId(), "ID");
        managementPcService.cancelRequest(request.getRequestHeaderId());
        return response;
    }
}
